﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;

namespace WindowsFormsApplication1
{
    public class Z : tetriminos
    {
        int coordinatesX;

        public int CoordinatesX
        {
            get { return coordinatesX; }
            set { coordinatesX = value; }
        }
        int coordinatesY;

        public int CoordinatesY
        {
            get { return coordinatesY; }
            set { coordinatesY = value; }
        }
        Color color;

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }
        int rotation;

        public int Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        /// <Constructor>
        /// 
        /// </Constructor>
        public Z()
        {
            coordinatesX = 4;
            coordinatesY = 1;
            rotation = 0;
            Color = Color.Green;
        }

        //gross fonction pour tout appeler d'un coup depuis form1.cs
        public int MyBigMoveFunction(String myvariable, PlayGrid THEgrid, int directionX, int directionY)
        {
            if (Form1.PartyGoing == true)
            {
                //test la collision
                if (testColision(THEgrid, directionX, directionY) == 0)
                {
                    //si pas de collision
                    //effacer
                    fil(THEgrid, false);
                    //changer les coordonnees
                    move(myvariable, THEgrid);
                    //remplir
                    fil(THEgrid, true);
                    return 1;
                }
                else if (testColision(THEgrid, directionX, directionY) == 2)
                {
                    return 1;
                }
                //sinon retourner 0 pour cree un nouvel objet
                else return 0;
            }
            return 1;
        }

        //function pour changer les coord, test les diff possibilités
        //manque plein de test
        public void move(String myvariable, PlayGrid THEgrid)
        {
            if (myvariable == "+")
            {
                //test les bords ET si la grille est colorée ou non
                //if((CoordinatesX > 0)&&(THEgrid.Grid[CoordinatesX - 1, CoordinatesY].Filled == false)) 
                CoordinatesX = CoordinatesX - 1;
            }
            else if (myvariable == "-")
            {
                //if ((CoordinatesX < 9)&&(THEgrid.Grid[CoordinatesX + 1, CoordinatesY].Filled == false)) 
                CoordinatesX = CoordinatesX + 1;
            }
            else if (myvariable == "b")
            {
                CoordinatesY = CoordinatesY + 1;
            }
        }

        //test la collision
        // return 2 if blocking collision
        // return 1 if final collision
        // return 0 if no collision
        public int testColision(PlayGrid THEgrid, int directionX, int directionY)
        {
            if(Form1.PartyGoing == true) {
            //////////////////////////////////////////////////////
            //////////////////FINAL COLLISION/////////////////////
            //////////////////////////////////////////////////////

            //test collision ligne du bas
            if ((directionY == 1) && (((this.CoordinatesY + 1 + directionY >= 20) && ((rotation == 0) || (rotation == 180)))))
            {
                return 1;
            }

            if ((directionY == 1) && (((this.CoordinatesY + directionY >= 20) && ((rotation == 90) || (rotation == 270)))))
            {
                return 1;
            }
            
            //si collision avec piece avec rotation 0 ou 180 et direction bas
            if ((directionY == 1) && ((rotation == 0) || (rotation == 180)) &&
                ((THEgrid.Grid[CoordinatesX + 1, CoordinatesY + 1 + directionY].Filled == true) ||
                (THEgrid.Grid[CoordinatesX, CoordinatesY + directionY].Filled == true)))
            {
                return 1;
            }
            //si collision avec piece et rotation 90 ou 270 et direction bas
            else if ((directionY == 1) && ((rotation == 90) || (rotation == 270)) &&
                ((THEgrid.Grid[CoordinatesX - 1, CoordinatesY + directionY].Filled == true) ||
                (THEgrid.Grid[CoordinatesX, CoordinatesY + directionY].Filled == true) ||
                (THEgrid.Grid[CoordinatesX + 1, CoordinatesY - 1 + directionY].Filled == true)))
            {
                return 1;
            }
            
            //////////////////////////////////////////////////////
            /////////////////BLOCKING COLLISION///////////////////
            //////////////////////////////////////////////////////
            //si collision avec bords et rotation 0 ou 180 et direction droite ou gauche
            else if (((rotation == 0) || (rotation == 180)) &&
                (((directionX == 1) && (CoordinatesX + 1 + directionX > 9)) ||
                ((directionX == -1) && (CoordinatesX + directionX < 0))))
            {
                return 2;
            }
            //si collision avec bords et rotation 90 ou 270 et direction droite ou gauche
            else if (((rotation == 90) || (rotation == 270)) &&
                (((directionX == 1) && (CoordinatesX + 1 + directionX > 9)) ||
                ((directionX == -1) && (CoordinatesX - 1 + directionX < 0))))
            {
                return 2;
            }
            //si collision avec piece et rotation 0 ou 180 et direction droite ou gauche
                ////////
                ////////
            else if (((rotation == 0) || (rotation == 180)) &&
                (((directionX == -1) &&
                ((THEgrid.Grid[CoordinatesX + directionX, CoordinatesY].Filled == true) ||
                (THEgrid.Grid[CoordinatesX + directionX, CoordinatesY + 1].Filled == true) ||
                (THEgrid.Grid[CoordinatesX + 1 + directionX, CoordinatesY + 1].Filled == true))) ||
                ((directionX == 1) &&
                ((THEgrid.Grid[CoordinatesX + 1 + directionX, CoordinatesY].Filled == true) ||
                (THEgrid.Grid[CoordinatesX + directionX, CoordinatesY - 1].Filled == true) ||
                (THEgrid.Grid[CoordinatesX + 1 + directionX, CoordinatesY + 1].Filled == true)))))
            {
                return 2;
            }
            //si collision avec piece et rotation 90 ou 270 et direction droite ou gauche
            else if (((rotation == 90) || (rotation == 270)) &&
                (((directionX == 1) && 
                ((THEgrid.Grid[CoordinatesX + 1 + directionX, CoordinatesY - 1].Filled == true) ||
                (THEgrid.Grid[CoordinatesX + directionX, CoordinatesY].Filled == true)) ||
                ((directionX == -1) && 
                ((THEgrid.Grid[CoordinatesX - 1 + directionX, CoordinatesY].Filled == true) ||
                (THEgrid.Grid[CoordinatesX + directionX, CoordinatesY - 1].Filled == true))))))
            {
                return 2;
            } else return 0;
            } else return 0;
        }

        //test if the rotation is legal
        //return true if legal
        //return false if not legal
        public bool rotateCop()
        {
            if ((this.rotation == 0) || (this.rotation == 180))
            {
                if ((coordinatesX + 1 > 10) || (coordinatesX - 1 < 0))
                {
                    return false;
                }
                else if (((Form1.THEgrid.Grid[CoordinatesX + 1, CoordinatesY - 1].Filled == true) ||
                (Form1.THEgrid.Grid[CoordinatesX - 1, CoordinatesY].Filled == true)))
                {
                    return false;
                }
            }
            else if ((this.rotation == 90) || (this.rotation == 270))
            {
                if (coordinatesY + 1 >= 20)
                {
                    return false;
                }
                else if (((Form1.THEgrid.Grid[CoordinatesX + 1, CoordinatesY].Filled == true) ||
                (Form1.THEgrid.Grid[CoordinatesX + 1, CoordinatesY + 1].Filled == true)))
                {
                    return false;
                }
            }

            return true;
        }

        //fonction pour effacet et remplir
        public void fil(PlayGrid THEgrid, bool anotherV)
        {

            if ((this.rotation == 0) || (this.rotation == 180))
            {
                    THEgrid.Grid[CoordinatesX, CoordinatesY].Filled = anotherV;
                    THEgrid.Grid[CoordinatesX, CoordinatesY].Color = this.Color;
                    THEgrid.Grid[CoordinatesX, CoordinatesY-1].Filled = anotherV;
                    THEgrid.Grid[CoordinatesX, CoordinatesY-1].Color = this.Color;
                    THEgrid.Grid[CoordinatesX+1, CoordinatesY].Filled = anotherV;
                    THEgrid.Grid[CoordinatesX+1, CoordinatesY].Color = this.Color;
                    THEgrid.Grid[CoordinatesX+1, CoordinatesY+1].Filled = anotherV;
                    THEgrid.Grid[CoordinatesX+1, CoordinatesY+1].Color = this.Color;
            }
            else if ((this.rotation == 90) || (this.rotation == 270))
            {
                THEgrid.Grid[CoordinatesX, CoordinatesY].Filled = anotherV;
                THEgrid.Grid[CoordinatesX, CoordinatesY].Color = this.Color;
                THEgrid.Grid[CoordinatesX - 1, CoordinatesY].Filled = anotherV;
                THEgrid.Grid[CoordinatesX - 1, CoordinatesY].Color = this.Color;
                THEgrid.Grid[CoordinatesX, CoordinatesY - 1].Filled = anotherV;
                THEgrid.Grid[CoordinatesX, CoordinatesY - 1].Color = this.Color;
                THEgrid.Grid[CoordinatesX + 1, CoordinatesY - 1].Filled = anotherV;
                THEgrid.Grid[CoordinatesX + 1, CoordinatesY - 1].Color = this.Color;
            }
        }

        //fonction pour rotation
        public void rotate()
        {
            if (rotateCop())
            {
                this.rotation = this.rotation + 90;
                if (this.rotation == 360)
                {
                    this.rotation = 0;
                }
            }
        }
    }
}